import 'package:flutter/material.dart';
import '../../../../widget/PPButtonView.dart';
import '../model/PPShopHomePageModel.dart';

class PPCommdityColumnItemView extends StatefulWidget {

    PPCommdityColumnItemView(PPCommodityModel data, { Key key, this.onClickAction }): this.data = data, super(key: key);

    final PPCommodityModel data;
    final Function onClickAction;

    _PPCommdityColumnItemViewState createState() => _PPCommdityColumnItemViewState();

}

class _PPCommdityColumnItemViewState extends State<PPCommdityColumnItemView> {

    PPCommodityModel data;

    @override
    void initState() {
        data = widget.data;
        super.initState();
    }

    @override
    void didUpdateWidget(PPCommdityColumnItemView oldWidget) {
        data = widget.data;
        super.didUpdateWidget(oldWidget);
    }

    @override
    Widget build(BuildContext context) {
        double screenWidth = MediaQuery.of(context).size.width;
        String cmmtdyPicUrl = data.cmmtdyPicUrl != null ? data.cmmtdyPicUrl : '';
        cmmtdyPicUrl = cmmtdyPicUrl.startsWith('http') ? cmmtdyPicUrl : 'https://' + cmmtdyPicUrl;

        return PPButtonView(
            btnMargin: EdgeInsets.only(top: 10),
            btnChild: Container(
                width: screenWidth/2.0 - 17.5,
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5)),
                child: Column(
                    children: <Widget>[
                        Container(
                            width: screenWidth/2.0 - 17.5,
                            height: screenWidth/2.0 - 17.5,
                            decoration: BoxDecoration(image: DecorationImage(image: AssetImage('resource/images/square_placeholder_img.jpg'))),
                            child: Image.network(cmmtdyPicUrl)
                        ),
                        Container(
                            padding: EdgeInsets.fromLTRB(10, 10, 10, 4),
                            child: Text(data.cmmdtyName, maxLines: 2, style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold, color: Color.fromRGBO(51, 51, 51, 1))),
                        ),
                        _getActivityWidget(screenWidth/2.0 - 37.5),
                        Container(
                            margin: EdgeInsets.fromLTRB(10, 10, 10, 2),
                            alignment: Alignment.centerLeft,
                            child: Text('¥' + data.cmmtdyPrice, style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Color.fromRGBO(102, 102, 102, 1))),
                        ),
                        _getInstInfoView()
                    ],
                ),
            ),
            onPressedAction: _onClickCommdityColumnItemAction,
        );
    }

    _getInstInfoView() {
        if(data != null && data.defaultInstInfo != null && data.defaultInstInfo.indexOf('|') > 0) {
            List<String> arr = data.defaultInstInfo.split('|');
            return (Container(
                margin: EdgeInsets.only(bottom: 9),
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                child: Row(
                    children: [
                        Text('¥' + arr[0], style: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, color: Color.fromRGBO(255, 170, 30, 1))),
                        Text('*' + arr[1] + '期', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, color: Color.fromRGBO(102, 102, 102, 1)))
                    ],
                )
            ));
        } else {
            return Container();
        }
    }

    _onClickCommdityColumnItemAction() {
        if(widget.onClickAction != null) {
            widget.onClickAction(data);
        }
    }

    _getActivityWidget(double w) {
        List<String> cmmdtyTagList = data.cmmdtyTagList;
        bool hasActivity =  _getIsSHowFreeActivity(data.promotionInfoList);
        bool hasTag = cmmdtyTagList != null && cmmdtyTagList.isNotEmpty;
        if(hasActivity || hasTag) {
            Widget titleWidget = _getActivityWidgetWithTitle(hasActivity, "免息活动");
            List<Widget>  imgWidgets = _getActivityWidgetWithImage(hasActivity, hasTag, cmmdtyTagList);
            imgWidgets = imgWidgets != null && imgWidgets.isNotEmpty ? imgWidgets : (titleWidget != null ? [titleWidget] : []);
            return Container(
                width: w,
                margin: EdgeInsets.only(top: 5),
                alignment: Alignment.center,
                child: Row(
                    children: imgWidgets
                )
            );
        } else {
            return Container();
        }
    }

    Widget _getActivityWidgetWithTitle(bool isShow, String title) {
        if(isShow) {
            return Container(
                width: 60,
                margin: EdgeInsets.only(right: 9),
                padding: EdgeInsets.fromLTRB(4, 1, 4, 1),
                height: 16,
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(50), color: Color(0xFF468CFF)),
                child: Text(title, maxLines: 1, textAlign: TextAlign.center, style: TextStyle(fontSize: 10, color: Colors.white))
            );
        } else {
            return Container();
        }
    }

    List<Widget> _getActivityWidgetWithImage(bool hasActivity, bool isShow, List<String> cmmdtyTagList) {
        if(isShow) {
            List<String> items = hasActivity && cmmdtyTagList.length > 1 ? cmmdtyTagList.sublist(0, 2) : cmmdtyTagList.sublist(0, 1);
                return items.map((item){
                    int index = items.indexOf(item);
                    return Container(
                        width: 60,
                        height: 15,
                        margin: EdgeInsets.only(right: index == 0 ? 9 : 0),
                        child: Image.network(item, fit: BoxFit.fill)
                    );
                }).toList();  
        } else {
            return [];
        }
    }

    _getIsSHowFreeActivity(List<Map> promotionInfoList) {
        if (promotionInfoList != null && promotionInfoList.length > 0) {
            bool hasActivity = false;
            promotionInfoList.retainWhere((item){
                String promotionType = item["promotionType"];
                return promotionType == '01';
            });
            hasActivity = promotionInfoList.length > 0;
            return hasActivity;
        } else {
            return false;
        }
    }

}